<template>
	<el-container>
		<el-aside width="200px" v-loading="showGrouploading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="group" class="menu" node-key="deptId" :data="group" :props="groupsProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
				<el-header>
					<div class="left-panel">
						<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
						<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
						<el-button type="primary" plain :disabled="selection.length!==1" @click="resetPd">密码重置</el-button>
						<el-button type="primary" plain :disabled="selection.length!==1" @click="setBaseInfo">基本信息</el-button>
						<el-button type="primary" plain @click="cloneAdminDesk">克隆桌面</el-button>
						<el-button type="danger" @click="setInitUser">初始化</el-button>
						<el-button type="success" icon="el-icon-download" @click="getExpUserInfoRecord"></el-button>
						<sc-upload-file :apiObj="this.$API.system.user.importUser" v-model="fileList" :limit="1" accept=".xlsx" style="margin:0px 10px 0px 10px;"
										:on-success="handleSuccess"
										:show-file-list="false">
							<el-button type="warning" icon="sc-icon-upload">导入</el-button>
						</sc-upload-file>
					</div>
					<div class="right-panel">
						<div class="right-panel-search">
							<el-input v-model="search.keyword" placeholder="账号 / 别名 / 姓名 / 电话 / 拼音" clearable></el-input>
							<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						</div>
					</div>
				</el-header>
				<el-main class="nopadding">
					<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter>
						<el-table-column type="selection" width="50"></el-table-column>
						<el-table-column label="#" type="index" width="50"></el-table-column>
						<el-table-column label="头像" width="80">
							<template #default="scope">
								<el-avatar :src="scope.row.avatar" size="small" style="display: flex"></el-avatar>
							</template>
						</el-table-column>
						<el-table-column label="登录账号" prop="accountId" width="150" sortable='custom'></el-table-column>
						<el-table-column label="姓名" prop="userName" width="100" sortable='custom'></el-table-column>
						<el-table-column label="用户昵称" prop="byName" width="120" sortable='custom'></el-table-column>
						<el-table-column label="别名" prop="alias" width="100" sortable='custom'></el-table-column>
						<el-table-column label="拼音" prop="pinYin" width="120" sortable='custom'></el-table-column>
						<el-table-column label="性别" prop="sex" width="60">
							<template #default="scope">
								<el-button type="primary" size="small" v-if="scope.row.sex==='1'">男</el-button>
								<el-button type="danger" size="small" v-else-if="scope.row.sex==='0'">女</el-button>
								<el-button type="info" size="small" v-else>保密</el-button>
							</template>
						</el-table-column>
						<el-table-column label="行政级别" prop="levelName" width="100"></el-table-column>
						<el-table-column label="联系电话" prop="phone" width="100" :show-overflow-tooltip="true"></el-table-column>
						<el-table-column label="微信账号" prop="wxAccountId" width="100" :show-overflow-tooltip="true"></el-table-column>
						<el-table-column label="钉钉账号" prop="ddAccountId" width="100" :show-overflow-tooltip="true"></el-table-column>
						<el-table-column label="人员来源" prop="isIn" width="100">
							<template #default="scope">
								<el-button type="warning" size="small" round v-if="scope.row.isIn==='1'">外部</el-button>
								<el-button type="success" size="small" round v-else-if="scope.row.isIn==='0'">内部</el-button>
								<el-button type="info" size="small" round v-else>未知</el-button>
							</template>
						</el-table-column>
						<el-table-column label="创建时间" prop="createTime" width="150"></el-table-column>
						<el-table-column label="最后登陆" prop="lastLoginTime" width="150" sortable='custom'></el-table-column>
						<el-table-column label="操作" fixed="right" align="right" width="220">
							<template #default="scope">
								<el-button-group>
									<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
									<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
									<el-popconfirm title="确定禁用吗？" @confirm="setUserStatus(scope.row, scope.$index)" v-if="scope.row.accountId!='admin'">
										<template #reference>
											<el-button text type="primary" size="small" v-if="scope.row.status=='0'">禁用</el-button>
											<el-button text type="primary" size="small" v-else>启用</el-button>
										</template>
									</el-popconfirm>
									<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)" v-if="scope.row.accountId!='admin'">
										<template #reference>
											<el-button text type="primary" size="small">删除</el-button>
										</template>
									</el-popconfirm>
								</el-button-group>
							</template>
						</el-table-column>
					</scTable>
				</el-main>
		</el-container>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>

	<resetPwd v-if="resetPwd.save" ref="resetPwdDialog" @success="handleSuccess" @closed="resetPwd.save=false"></resetPwd>

	<userbaseinfo v-if="userbase.save" ref="userbaseinfo" @success="handleSuccess" @closed="userbase.save=false"></userbaseinfo>
</template>

<script>
	import saveDialog from './save'
	import resetPwd from './resetpwd'
	import userbaseinfo from "./userbaseinfo";
	import config from "@/config";

	export default {
		name: 'user',
		components: {
			userbaseinfo,
			saveDialog,
			resetPwd
		},
		data() {
			return {
				dialog: {
					save: false
				},
				fileList:[],
				resetPwd:{
					save: false
				},
				userbase:{
					save: false
				},
				showGrouploading: false,
				groupFilterText: '',
				group: [],
				groupsProps: {
					label:"deptName",
					value: "deptId"
				},
				apiObj: this.$API.system.user.list,
				selection: [],
				search: {
					keyword: null
				}
			}
		},
		watch: {
			groupFilterText(val) {
				this.$refs.group.filter(val);
			}
		},
		mounted() {
			this.getDeptGroup()
		},
		methods: {
			getExpUserInfoRecord(){
				this.$API.system.user.getExpUserInfoRecord.fileDownload();
			},
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			setBaseInfo(){
				if(this.selection.length>1)
				{
					this.$alert("一次不能同时为多个用户设置！", "提示", {type: 'error'})
				}
				var thisRow;
				this.selection.forEach(item => {
					thisRow = item;
				})
				this.userbase.save = true;
				this.$nextTick(() => {
					this.$refs.userbaseinfo.open().setData(thisRow);
				})
			},
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//查看
			table_show(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//删除
			async table_del(row, index){
				const loading = this.$loading();
				var reqData = {accountId: row.accountId}
				var res = await this.$API.system.user.delete.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh();
					this.$message.success(res.message);
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			},
			async setUserStatus(row, index)
			{
				const loading = this.$loading();
				var reqData = {accountId: row.accountId,status:row.status=='0'?"1":"0"}
				var res = await this.$API.system.user.updateUserInfo.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh();
					this.$message.success(res.message);
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			},
			//重置密码
			resetPd(){
				var userJson = {};
				for (var i = 0; i < this.selection.length; i++) {
					userJson.accountId = this.selection[i].accountId;
					userJson.userName = this.selection[i].userName;
				}
				this.resetPwd.save = true
				this.$nextTick(() => {
					this.$refs.resetPwdDialog.open().setData(userJson);
				})
			},
			//批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(async() => {
					const loading = this.$loading();
					var idArr=[];
					this.selection.forEach(item => {
						idArr.push(item.accountId);
					})
					var res = await this.$API.system.user.deleteByAccountIds.post({accountIds: idArr});
					if (res.code == 200) {
						this.$refs.table.refresh()
						this.$message.success(res.message)
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
					loading.close();
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//加载树数据
			async getDeptGroup(){
				this.showGrouploading = true;
				var res = await this.$API.system.unitDept.list.get();
				this.showGrouploading = false;
				var allNode ={deptId: '', deptName: '所有'}
				res.data.unshift(allNode);
				this.group = res.data;
			},
			//树过滤
			groupFilterNode(value, data){
				if (!value) return true;
				return data.deptName.indexOf(value) !== -1;
			},
			//树点击事件
			groupClick(data){
				var params = {
					deptId: data.deptId
				}
				this.$refs.table.reload(params)
			},
			//搜索
			upsearch(){
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSuccess(data, mode){
				this.$refs.table.refresh()
			},
			async cloneAdminDesk()
			{
				var res = await this.$API.system.user.cloneAdminDesk.post();
				if(res.code==200)
				{
					this.$message.success(res.message)
				}else
				{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			async setInitUser()
			{
				this.$confirm(`您确定要初始化所有账号吗？`, '提示', {
					type: 'warning'
				}).then(async() => {
					var res = await this.$API.system.user.setInitUser.post();
					if(res.code == 200){
						this.$refs.table.refresh()
						this.$message.success(res.message)
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}).catch(() => {

				})
			},
		}
	}
</script>

<style>
</style>
